package com.wt.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.entity.WqunCommunity;
import com.wt.admin.entity.WqunCommunityBan;
import com.wt.admin.service.WqunCommunityBanService;
import com.wt.admin.service.impl.WqunCommunityBanServiceImpl;
import com.wt.commonutils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-09-20
 */
@RestController
@RequestMapping("/wqunCommunityBan")
public class WqunCommunityBanController {

    @Autowired
    public WqunCommunityBanService wqunCommunityBanService = new WqunCommunityBanServiceImpl();


    //添加禁言
    @ApiOperation("添加禁言")
    @GetMapping("/addBan/{id}")
    @PreAuthorize("hasAnyAuthority('admin:wqunCommunityBan:addBan')")
    public R addBan(@PathVariable("id") String id){

        //判断是否已经被禁言了
        QueryWrapper<WqunCommunityBan> queryWrapper  =new QueryWrapper<>();
        queryWrapper.eq("uid",id);
        queryWrapper.eq("is_ban",1);
        int banCount = wqunCommunityBanService.count(queryWrapper);
        if(banCount>=1){
            return R.ok();
        }
        //禁言
        WqunCommunityBan wqunCommunityBan = new WqunCommunityBan();
        wqunCommunityBan.setUid(id);
        wqunCommunityBan.setIsBan("1");

        boolean save = wqunCommunityBanService.save(wqunCommunityBan);
        return save?R.ok().message("禁言成功"):R.error().message("禁言失败");
    }

    //解除禁言
    @ApiOperation("解除禁言")
    @GetMapping("/deleteBan/{id}")
    @PreAuthorize("hasAnyAuthority('admin:wqunCommunityBan:deleteBan')")
    public R deleteBan(@PathVariable("id") String id){

        QueryWrapper<WqunCommunityBan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",id);
        queryWrapper.eq("is_ban",1);
        WqunCommunityBan byId = wqunCommunityBanService.getOne(queryWrapper);
        if(byId != null){
            byId.setIsBan("0");
            boolean b = wqunCommunityBanService.updateById(byId);
            return b?R.ok().message("解除禁言成功"):R.error().message("解除禁言失败");
        }
        return R.error().message("该用户未被禁言");
    }
}

